﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.Models;

namespace VehicleScale.EntityMappers
{
    public class EmployeeMapper
    {

        #region Methods With Relation
        public static EmployeeModel ToModel(Employee entity)
        {
            if (entity == null) return null;

            EmployeeModel model = new EmployeeModel()
            {
                EmployeeID = entity.EmployeeID,
                EmployeeName = entity.EmployeeName,
                Telephone = entity.Telephone,
                Address = entity.Address,
                Description = entity.Description,
                Identification = entity.Identification,
                CreatedByID = entity.CreatedByID,
                CreationDate = entity.CreationDate,
                ModifiedByID = entity.ModifiedByID,
                ModifiedDate = entity.ModifiedDate,
                Active = entity.Active,
                Username = entity.Username,
                Password = entity.Password,
                GroupPermissionID = entity.GroupPermissionID
                
            };

            if (entity.VehicleScales != null)
            {
                model.VehicleScales = new List<VehicleScaleModel>();
                foreach (var item in entity.VehicleScales)
                {
                    model.VehicleScales.Add(VehicleScaleMapper.ToModel(item, 0));
                }
            }

            return model;
        }
        public static Employee ToEntity(Employee entity, EmployeeModel model)
        {
            if (model == null) return null;

            entity.EmployeeID = model.EmployeeID;
            entity.EmployeeName = model.EmployeeName;
            entity.Telephone = model.Telephone;
            entity.Address = model.Address;
            entity.Description = model.Description;
            entity.Identification = model.Identification;
            entity.CreatedByID = model.CreatedByID;
            entity.CreationDate = model.CreationDate;
            entity.ModifiedByID = model.ModifiedByID;
            entity.ModifiedDate = model.ModifiedDate;
            entity.Active = model.Active;
            entity.Username = model.Username;
            entity.Password = model.Password;
            entity.GroupPermissionID = model.GroupPermissionID;
            
            return entity;
        } 
        #endregion

        #region Methods Without Relation
        public static EmployeeModel ToModel(Employee entity, int childLevel)
        {
            if (entity == null) return null;

            EmployeeModel model = new EmployeeModel()
            {
                EmployeeID = entity.EmployeeID,
                EmployeeName = entity.EmployeeName,
                Telephone = entity.Telephone,
                Address = entity.Address,
                Description = entity.Description,
                Identification = entity.Identification,
                CreatedByID = entity.CreatedByID,
                CreationDate = entity.CreationDate,
                ModifiedByID = entity.ModifiedByID,
                ModifiedDate = entity.ModifiedDate,
                Active = entity.Active,
                Username = entity.Username,
                Password = entity.Password
            };

            if (childLevel > 0)
            {
                if (entity.VehicleScales != null)
                {
                    model.VehicleScales = new List<VehicleScaleModel>();
                    foreach (var item in entity.VehicleScales)
                    {
                        model.VehicleScales.Add(VehicleScaleMapper.ToModel(item, childLevel - 1));
                    }
                }
            }

            return model;
        }
        
        #endregion

    }
}
